#!/usr/bin/env julia

"""
Basic functionality test for GSI Core Analysis package.

This script tests the main components of the package to verify they load
correctly and basic operations work as expected.
"""

using GSICoreAnalysis
using LinearAlgebra
using Printf

println("="^60)
println("GSI Core Analysis - Basic Functionality Test")
println("="^60)

# Test 1: Basic type creation
println("\n1. Testing basic type creation...")
try
    config = AnalysisConfig(
        grid_size=(100, 80, 32),
        ensemble_size=20,
        hybrid_coeff=0.6,
        max_iterations=100,
        convergence_tol=1e-6
    )
    println("✓ AnalysisConfig created successfully")
    println("  Grid size: $(config.grid_size)")
    println("  Ensemble size: $(config.ensemble_size)")
    println("  Hybrid coefficient: $(config.hybrid_coeff)")
catch e
    println("✗ Failed to create AnalysisConfig: $e")
end

# Test 2: Grid operations
println("\n2. Testing grid operations...")
try
    dims = GridDimensions(100, 80, 32)
    println("✓ GridDimensions created successfully")
    println("  nx=$(dims.nx), ny=$(dims.ny), nz=$(dims.nz)")
    println("  Total grid points: $(dims.total)")
catch e
    println("✗ Failed to create GridDimensions: $e")
end

# Test 3: Analysis variables
println("\n3. Testing analysis variables...")
try
    vars = default_analysis_variables()
    println("✓ Default analysis variables created")
    println("  Number of variables: $(length(vars.variables))")
    println("  Total size: $(vars.total_size)")
    for var in vars.variables[1:5]  # Show first 5
        println("    $(var.name): $(var.levels) levels, indices $(var.start_index)-$(var.end_index)")
    end
catch e
    println("✗ Failed to create analysis variables: $e")
end

# Test 4: ControlVector creation (if available)
println("\n4. Testing ControlVector...")
try
    using GSICoreAnalysis.ControlVectors
    config = AnalysisConfig()
    # Try to create a control vector - this might fail if constructor needs more parameters
    println("✓ ControlVectors module loaded")
catch e
    println("✗ ControlVector test failed: $e")
end

# Test 5: Background error modules
println("\n5. Testing BackgroundError modules...")
try
    using GSICoreAnalysis.BackgroundError
    using GSICoreAnalysis.BackgroundError.RecursiveFiltering
    using GSICoreAnalysis.BackgroundError.SpectralMethods
    using GSICoreAnalysis.BackgroundError.BalanceOperators
    using GSICoreAnalysis.BackgroundError.AnisotropicFiltering
    using GSICoreAnalysis.BackgroundError.CovarianceLocalization
    using GSICoreAnalysis.BackgroundError.FlowDependentCorrelations
    println("✓ All BackgroundError modules loaded successfully")
catch e
    println("✗ BackgroundError modules test failed: $e")
end

# Test 6: Advanced solvers
println("\n6. Testing AdvancedSolvers...")
try
    using GSICoreAnalysis.AdvancedSolvers
    println("✓ AdvancedSolvers module loaded")
    
    # Test creating solver configuration
    config = create_default_configs()
    println("✓ Default solver configurations created")
    println("  Available solvers: $(keys(config))")
catch e
    println("✗ AdvancedSolvers test failed: $e")
end

# Test 7: Cost function module
println("\n7. Testing CostFunctions...")
try
    using GSICoreAnalysis.CostFunctions
    println("✓ CostFunctions module loaded")
catch e
    println("✗ CostFunctions test failed: $e")
end

# Test 8: State vector operations
println("\n8. Testing StateVectors...")
try
    using GSICoreAnalysis.StateVectors
    println("✓ StateVectors module loaded")
catch e
    println("✗ StateVectors test failed: $e")
end

# Test 9: Grid operations
println("\n9. Testing GridOperations...")
try
    using GSICoreAnalysis.GridOperations
    println("✓ GridOperations module loaded")
catch e
    println("✗ GridOperations test failed: $e")
end

# Test 10: Minimization algorithms
println("\n10. Testing Minimization...")
try
    using GSICoreAnalysis.Minimization
    println("✓ Minimization module loaded")
catch e
    println("✗ Minimization test failed: $e")
end

println("\n" * "="^60)
println("Basic functionality test completed!")
println("="^60)